package com.example.task1.ui.AllService.life;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.LifeBannerBean;
import com.example.task1.bean.LifeTabBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;

public class LifeActivity extends AppCompatActivity {

    private Banner banner;
    private TextView tvFeedback;
    private TextView tvPlay;
    private TextView tvWeather;
    private TabLayout tabLayout;
    private ViewPager pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("生活缴费");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        initData();
        business();
    }

    private void business() {
        tvPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.show("当前模块还未开放~");
            }
        });
        tvFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(LifeActivity.this,LifeFeedbackActivity.class);
            }
        });
        tvWeather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(LifeActivity.this,LifeWeatherActivity.class);
            }
        });
    }

    private void initData() {
        Call<ApiResp> bannerCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/living/rotation/list");
        Api.send(bannerCall, LifeBannerBean.class, new CallBack<LifeBannerBean>() {
            @Override
            public void success(LifeBannerBean data) {
                List<String> collect = data.getRows().stream().map(LifeBannerBean.RowsDTO::getAdvImg).collect(Collectors.toList());
                MyApplication.banner(banner,collect);
            }

            @Override
            public void error(String msg) {

            }
        });
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/living/press/category/list");
        Api.send(apiRespCall, LifeTabBean.class, new CallBack<LifeTabBean>() {
            @Override
            public void success(LifeTabBean data) {
                LifeTabAdapter lifeTabAdapter = new LifeTabAdapter(getSupportFragmentManager(),data.getData());
                tabLayout.setupWithViewPager(pager);
                pager.setAdapter(lifeTabAdapter);
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        banner = (Banner) findViewById(R.id.banner);
        tvFeedback = (TextView) findViewById(R.id.tv_feedback);
        tvPlay = (TextView) findViewById(R.id.tv_play);
        tvWeather = (TextView) findViewById(R.id.tv_weather);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        pager = (ViewPager) findViewById(R.id.pager);
    }
}